package zero.tech.games.gate.error;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import zero.tech.core.data.Resp;
import zero.tech.games.common.Code;

@RestControllerAdvice
public class ErrorController {
	@ExceptionHandler(Exception.class)
    public Resp errorResult(Exception e) {
		String msg=e.getMessage();
		if(e instanceof IllegalStateException &&StringUtils.isNotBlank(msg)&&msg.startsWith("No instances available for")) {
			return Resp.build(Code.SERVER_NOT_OPEN, "服务不可用,请稍后再试");
		}
		if(e instanceof RuntimeException && e.getCause()!=null&&e.getCause().getMessage().startsWith("Load balancer does not have available server for client")) {
			return Resp.build(Code.SERVER_NOT_OPEN, "服务不可用,请稍后再试");
		}
		e.printStackTrace();
		return Resp.build(Code.SERVER_ERROR,"服务器异常,稍后再试");
    }
}
